/*
 * @Author: 陶之群
 * @Date: 2020年4月15日11:17:27
 * @Last Modified by: 陶之群
 * @Last Modified time: 2020年4月15日11:17:27
 */
import React, { createElement } from 'react';
import Loadable from 'react-loadable';
import { Spin } from 'antd';

const modelNotExisted = (app, model) =>
    !app._models.some(({ namespace }) => {
        return namespace === model.substring(model.lastIndexOf('/') + 1);
    });
const dynamicWrapper = (app, models, component) => {
    models.forEach(model => {
        if (modelNotExisted(app, model)) {
            app.model(require(`../models/${model}`).default);
        }
    });
    if (component.toString().indexOf('.then(') < 0) {
        return props => {
            return createElement(component().default, {
                ...props
            });
        };
    }
    return Loadable({
        loader: () => {
            return component().then(raw => {
                const Component = raw.default || raw;
                return props =>
                    createElement(Component, {
                        ...props
                    });
            });
        },
        loading: ({ error, pastDelay }) => {
            if (pastDelay) {
                return <Spin size="large" className="global-spin" />;
            } else {
                return null;
            }
        },
    });
};

export const getRouterData = app => ([
    {
        path: "/black/page2",
        component: dynamicWrapper(app, [], () => import("../routes/Qps.js")),
    },
    {
        path: "/",
        component: dynamicWrapper(app, [], () => import("../layouts/BasicLayout.js")),
        routes: [
            // {
            //     path: "/page1",
            //     component: dynamicWrapper(app, ["example1"], () => import("../routes/Page1.js"))
            // },
            {
                path: "/qps",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/Qps.js")),
            },
            {
                path: "/tps",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/Tps.js"))
            },
            {
                path: "/selectqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/SelectQty.js"))
            },
            {
                path: "/insertqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/InsertQty.js"))
            },
            {
                path: "/updateqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/UpdateQty.js"))
            },
            {
                path: "/deleteqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/DeleteQty.js"))
            },
            {
                path: "/writeqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/WriteQty.js"))
            },
            {
                path: "/innodbbufferpool",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/InnodbBufferPool.js"))
            },
            {
                path: "/myisambufferpool",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/MyisamBufferPool.js"))
            },
            {
                path: "/receivedflux",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/ReceivedFlux.js"))
            },
            {
                path: "/sentflux",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/SentFlux.js"))
            },
            {
                path: "/slowqueries",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/SlowQueries.js"))
            },
            {
                path: "/threadconnedqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/ThreadConnedQty.js"))
            },
            {
                path: "/threadconningqty",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/ThreadConningQty.js"))
            },
            {
                path: "/cpuusedrate",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/CpuUsedRate.js"))
            },
            {
                path: "/memoryusedrate",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/MemoryUsedRate.js"))
            },
            {
                path: "/diskusedrate",
                component: dynamicWrapper(app, ["example2"], () => import("../routes/DiskUsedRate.js"))
            },
            ]}
]);
